using Assets.Scripts.DL.Consts;
using Assets.Scripts.DL.Mgrs;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Wind : MonoBehaviour {
	//[SerializeField] Rigidbody2D rb;
	Timer timer;
	[SerializeField] SpriteRenderer sp;
	[SerializeField] float KnockFlySpeed;
	Vector3 v;
	bool _isknockPlayer;
	public void Init(Vector2 v, float flytime, Vector3 pos) {
		if (timer == null) {
			timer = new Timer();
		}
		this.v = v;
		transform.position = pos;
		//rb.velocity = v;
		StartCoroutine(Fly(flytime));
		_isknockPlayer = false;
	}

	private void Update() {
		transform.position += v * Time.deltaTime;
	}

	IEnumerator Fly(float flytime) {
		yield return new WaitForSeconds(flytime);
		v = Vector3.zero;
		while (sp.color.a > 0.1f) {
			var c = sp.color;
			c.a = Mathf.Lerp(c.a, 0, 0.1f);
			sp.color = c;
			yield return null;
		}
		ObjectPool.Instance.RecycleObject(ObjPoolStr.Wind, gameObject);
	}

	private void OnTriggerEnter2D(Collider2D collision) {
		if (collision.CompareTag(Tags.Player) && !_isknockPlayer) {
			_isknockPlayer = true;
			var script = collision.GetComponent<PlayerController>();
			script.Velocity = new Vector2(0, KnockFlySpeed);
			script.StateData.IsKnockDown = true;
			script.StateData.IsKnockFly = true;
			//Debug.Log("Knock");
		}
	}
}
